package top.songxs.gmall.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.songxs.gmall.common.result.Result;
import top.songxs.gmall.common.result.ResultCodeEnum;
import top.songxs.gmall.order.biz.OrderBizService;
import top.songxs.gmall.order.dto.OrderSubmitDTO;
import top.songxs.gmall.order.entity.OrderInfo;
import top.songxs.gmall.order.service.OrderInfoService;
import top.songxs.gmall.ware.entity.WareStockMsg;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("api/order")
public class OrderController {

    @Autowired
    private OrderInfoService orderInfoService ;

    @Autowired
    private OrderBizService orderBizService;

    // 返回订单号
    @PostMapping("/auth/submitOrder")
    public Result<String> orderSubmit( @Valid @RequestBody OrderSubmitDTO orderSubmitDTO ,
                                      @RequestParam(value = "tradeNo") String tradeNo) {
        String orderId = orderInfoService.orderSubmit(orderSubmitDTO , tradeNo) ;
        return Result.ok(orderId) ;
    }

    //库存系统发来一个拆单请求
    @PostMapping(value = "/orderSplit")
    public List<WareStockMsg> orderSpilt(@RequestParam(value = "orderId") Long orderId , @RequestParam(value = "wareSkuMap") String wareSkuMapJson) {
        List<WareStockMsg> wareStockMsgList = orderBizService.orderSpilt(orderId , wareSkuMapJson);
        return wareStockMsgList ;
    }


    // com.atguigu.gmall.order.controller.OrderController
    @GetMapping("/auth/{page}/{limit}")
    public Result orderList(@PathVariable("page") Long page,
                            @PathVariable("limit") Long limit){
        Page<OrderInfo> orderInfoPage = orderInfoService.pageOrderList(page , limit);
        return Result.build(orderInfoPage , ResultCodeEnum.SUCCESS) ;
    }



}
